const express = require('express')
const userRouter = express.Router() // 路由对象


// 用户列表
const userList = [
    {username:'admin',password:'123',age:18,sex:'男'}
]

//创建一个路由
/**
 *   username:'admin'
 *   password:'123'
 *   age:18,
 *   sex:'男'
 */
userRouter.get('/register',(req,res)=>{
    const username = req.query.username
    const password = req.query.password
    const age = req.query.age
    const sex = req.query.sex

    // 注册保存用户到数组
    userList.push({
        username,
        password,
        age,
        sex
    })

    res.send({
        code:1,
        message:'注册成功!'
    })

})


/**
 * 登录
 */
userRouter.get('/login',(req,res)=>{
    const username = req.query.username
    const password = req.query.password

    const user = userList.find(item=>item.username == username && item.password == password)
    if(user){
        res.send({
            code:1,
            message:'登录成功'
        })
    }else{
        res.send({
            code:-1,
            message:'用户名或者密码出错！'
        })
    }
})


module.exports = {
    userRouter
}